define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            var window_innerheight = window.innerHeight;
            var table_height = window_innerheight-75;
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'auth/admin/index',
                    add_url: 'auth/admin/add',
                    edit_url: 'auth/admin/edit',
                    del_url: 'auth/admin/del',
                    multi_url: 'auth/admin/multi',
                },
                pageSize: Config.pagesize,
                height: table_height
            });

            var table = $("#table");

            //在表格内容渲染完成后回调的事件
            table.on('post-body.bs.table', function (e, json) {
                $("tbody tr[data-index]", this).each(function () {
                    if (parseInt($("td:eq(1)", this).text()) == Config.admin.id) {
                        $("input[type=checkbox]", this).prop("disabled", true);
                    }
                });
            });
            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#','');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.group_id = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {field: 'id', title: 'ID',operate: false},

                        {field: 'name', title: __('the_name'),operate: 'like'},
                        {field: 'username', title: __('Username'),operate: 'like'},



                        {field: 'groups_text', title: __('Group'), operate:false, formatter: Table.api.formatter.label},
                        {field: 'avatar', title: __('Cover'),operate: false,formatter: Controller.api.formatter.showAvatar},
                        {field: 'mobile', title: __('Mobile'),operate: 'like'},
                        {field: 'status', title: __("Status"), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},

                        // {field: 'sort', title: __('Sort'), operate:false},
                        {field: 'logintime', title: __('Login time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange'},
                        {field: 'operate', title: __('Operate'), table: table,

                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                if(row.id == 1){
                                    return '';
                                }
                                return Table.api.formatter.operate.call(this, value, row, index);
                            }}
                    ]
                ]
            });


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        addteacher: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        setyongjin: function () {
            Controller.api.bindevent();
        },
        api:{
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                showAvatar:function (value, row, index) {
                    return '<a class="" href="'+row.avatar+'" target="_blank"><img src="'+row.avatar+'" alt="" style="width: 30px;height: 30px;"></a>';
                }
            }
        }
    };
    return Controller;
});
